from flask_restx import Resource, Namespace
from .. import api
from flask import jsonify, Blueprint
from . import api_bp, api
from . import auth
from . import users

# 创建命名空间
ns = api.namespace('demo', description='示例接口')

# 注册认证相关路由
api_bp.register_blueprint(auth.bp)

# 注册用户管理相关路由
api_bp.register_blueprint(users.ns.blueprint)

@ns.route('/hello')
class HelloResource(api.Resource):
    def get(self):
        """返回一个简单的问候消息"""
        return {"message": "Hello, MVP!"}

# 基础错误处理
@api_bp.errorhandler(404)
def handle_404_error(error):
    return jsonify({"error": "资源不存在"}), 404

@api_bp.errorhandler(500)
def handle_500_error(error):
    return jsonify({"error": "服务器内部错误"}), 500

# 健康检查接口
health_ns = Namespace('health', description='健康检查')
api.add_namespace(health_ns, path='/health')

@health_ns.route('')
class HealthCheck(Resource):
    def get(self):
        """健康检查接口"""
        return {'status': 'ok'}